#!/usr/bin/env python

from optparse import OptionParser
import sys, os, mpghelp

class ArgParser(object):
    def __init__(self):
        parser = OptionParser(add_help_option=False)
        parser.add_option('-s', '--sites', dest='sites', default='proxy_sites.txt')
        parser.add_option('-p', '--proxies', dest='proxies')
        parser.add_option('-t', '--threads', dest='sthreads', default=20)
        parser.add_option('-o', '--output', dest='output')
        parser.add_option('--test-proxies', dest='test_types')
        parser.add_option('--threads-proxies', dest='pthreads', default=20)
        parser.add_option('--whois', action='store_true', dest='whois', default=False)
        parser.add_option('-q', '--quiet', action='store_true', dest='quiet', default=False)
        parser.add_option('-h', '--help', action='store_true', dest='help', default=False)
        #parser.add_option('-v', '--verbose', action='store_true', dest='verbose', default=False)
        (self.options, args) = parser.parse_args()

        # Checking for help call
        if self.options.help == True:
            print mpghelp.helpscreen()
            sys.exit(0)

        # Making storage for errors
        self.errors = {'sites': [False, '\n[-] Error: --sites flag found without value'],
                       'proxies': [False, '\n[-] Error: --proxies flag found without value'],
                       'threads': [False, '\n[-] Error: value for threads needs to be an integer'],
                       'output': [False, '\n[-] Error: Cannot write to specified output'],
                       'test_types':[False, '\n Error: Invalid options for --test-proxies']}

        # Getting all arguments
        self.arglist = sys.argv[1:]

        # Checking arguments
        self.sites = self.checkFile('sites', self.options.sites)
        self.proxies = self.checkFile('proxies', self.options.proxies)
        self.sthreads = self.checkNumber('threads', self.options.sthreads)
        self.pthreads = self.checkNumber('threads', self.options.pthreads)
        self.output = self.checkWriteable(self.options.output)
        self.test_types = self.checkTestTypes(self.options.test_types)

    def checkFile(self, what, checkthis):
        if checkthis != None:
            if os.path.exists(checkthis):
                return ('file', checkthis)
            else:
                return ('value', checkthis)
        else:
            if what == 'sites':
                if ('-s' in self.arglist) or ('--sites' in self.arglist):
                    self.errors['sites'][0] = True
            elif what == 'proxies':
                if ('-p' in self.arglist) or ('--proxies' in self.arglist):
                    self.errors['proxies'][0] = True
            else:
                pass
            return None
            
    def checkNumber(self, what, number):
        if number != None:
            try:
                n = int(number)
                return n
            except ValueError:
                self.errors['threads'][0] = True
                return None
            else:
                if what == 'threads':
                    self.errors['threads'][0] = True
                else:
                    return None
        else:
            if ('-t' in self.arglist) or ('--threads' in self.arglist) or ('--threads-proxies' in self.arglist):
                self.errors['threads'][0] == True
            return None

    def checkWriteable(self, checkthis):
        if checkthis != None:
            try:
                tfile = open(checkthis)
                tfile.close()
            except IOError:
                if what == 'output':
                    self.errors['output'][0] = True
                else:
                    return None
            else:
                return checkthis
        else:
            return None

    def checkTestTypes(self, checkthis):
        testtypes = ('http', 'https', 'online', 'tcp-con', 'socks4', 'socks4a', 'socks5')
        totest = []
        if checkthis != None:
            checklist = checkthis.split(',')
            for i in checklist:
                if i in testtypes:
                    totest.append(i)
                else:
                    self.errors['test_types'][0] = True
                    return None
            return totest
        else:
            return None
            
        
    def getArguments(self):
        # Returning arguments to main class
        return {'sites':self.sites,
                'proxies':self.proxies,
                'sthreads':self.sthreads,
                'pthreads':self.pthreads,
                'output':self.output,
                'test_types':self.test_types,
                'whois':self.options.whois,
                'quiet':self.options.quiet}
            
